package com.runmi.webchat.aftersale.web.setup.controller;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;


import com.runmi.webchat.aftersale.common.result.Result;
import com.runmi.webchat.aftersale.common.result.ResultFactory;
import com.runmi.webchat.aftersale.common.utils.StringUtil;
import com.runmi.webchat.aftersale.dbcore.dao.SetupItemDao;
import com.runmi.webchat.aftersale.web.base.BaseController;
import com.runmi.webchat.aftersale.web.set.model.UserEdit;
import com.runmi.webchat.aftersale.web.setup.service.SetupItemService;
import com.runmi.webchat.aftersale.web.setup.service.SetupOrderService;
import com.runmi.webchat.aftersale.web.setup.vo.SetupItemEdit;
import com.runmi.webchat.aftersale.web.setup.vo.SetupItemImportTemp;
import com.runmi.webchat.aftersale.web.setup.vo.SetupItemReqVo;
import com.runmi.webchat.aftersale.web.setup.vo.SetupOrderResVo;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@Controller
@RequestMapping(value = "/admin/setupitem")
public class SetupItemController extends BaseController{

	@Autowired
	private SetupItemService setupItemService;
	
	

	@RequestMapping(value = "/index")
	public ModelAndView index(HttpServletRequest request,HttpServletResponse response,SetupItemReqVo model){
		ModelAndView mav = new ModelAndView("/setup/itemindex");		
		setupItemService.index(model);
    	mav.addObject("model", model);
        return mav;
		//return "/customer/Customer/seach";
	}
	
	/**
	 * 跳转到导入PO订单页面
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/importxls")
	public ModelAndView importxls(HttpServletRequest request,HttpServletResponse response){
		ModelAndView mav = new ModelAndView("/setup/import");
		return mav;
	}
	
	
	/**
	 * 执行导入PO订单文件
	 * @param request
	 * @param response
	 * @param file
	 * @param fileFlag
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping(value = "/importfile")
	@ResponseBody
	public Result importfile(
			HttpServletRequest request,HttpServletResponse response,
			@ApiParam(value = "file ") @RequestPart(value = "file", required = false) MultipartFile file,
			String fileFlag
	) throws ServletException, IOException {
		Integer userId=  this.getUserId(request,response);
		Result result = null;
		if(null==userId){
			result = ResultFactory.getResultFail("权限不足");
			return result;
		}

		String path = request.getSession().getServletContext().getRealPath("upload");
		String fileName = file.getOriginalFilename();
		if(StringUtil.isBlank(fileName)){
			result = ResultFactory.getResultFail( "创建文件夹:"+path+"失败");
			return result;
		}

		if(fileName.indexOf("\\")>-1){
			String fs[]=fileName.split("\\\\");
			fileName=fs[fs.length-1];
		}
		// 获得文件后缀名
		String suffix=fileName.substring(fileName.lastIndexOf(".")+1);

		if(StringUtil.isNotEmptyStr(fileFlag)){
			if(fileFlag.equals("1")){
				if(!suffix.equals("xlsx") && !suffix.equals("xlx")){
					result = ResultFactory.getResultFail( "只能上传Excel文件");
					return result;
				}
			}
		}
		result = setupItemService.importfile(file);
		if(result.isSuccess()){
			List<SetupItemImportTemp> createImportTempList = (List<SetupItemImportTemp>)result.getItems();
			try {
				setupItemService.insertDbFromImportFile(createImportTempList, getUserInfo(request, response));
			} catch (Exception e) {
				result = ResultFactory.getResultFail(e.getMessage());
			}
		}
		return result;
	}
	
	
	@RequestMapping(value = "/changeStatus")
	@ResponseBody
	public Map<String,String> changeStatus(HttpServletRequest request,HttpServletResponse response,Integer id,Byte opType){
	
		Map<String,String> result=setupItemService.changeStatus(this.getUserInfo(request, response),id,opType);
		return result;
	}
	
	@RequestMapping(value = "/del")
	@ResponseBody
	public Result del(HttpServletRequest request,HttpServletResponse response,Integer id){	
		Result result=setupItemService.del(this.getUserInfo(request, response),id);
		return result;
	}
	
	@RequestMapping(value = "/edit")
	public ModelAndView edit(HttpServletRequest request,HttpServletResponse response,SetupItemEdit model){
		//model.setUserId(this.getUserId(request, response));
		ModelAndView mav = new ModelAndView("/setup/edititem");		
		setupItemService.edit(model);
    	mav.addObject("model",model);
        return mav;
		//return "/customer/Customer/seach";
	}
	
	@RequestMapping(value = "/save")
	@ResponseBody
	public Map<String,String> save(HttpServletRequest request,HttpServletResponse response,SetupItemEdit model){
		Map<String,String> result=setupItemService.save(model);
		return result;
	}
	
	
}
